package Form;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JPanel;


public class Shop_Customization_Canvas extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private ArrayList <Shop_Customization_PictureSettings> customizations = new ArrayList <Shop_Customization_PictureSettings> ();
	private ArrayList <Shop_Customization_TextSettings> textCustomizations = new ArrayList <Shop_Customization_TextSettings> ();
	private boolean front = false, back = false;
	
	public Shop_Customization_Canvas(boolean side){
		if(side){
			setFront(true);
			setBack(false);
		}
		else{
			setBack(true);
			setFront(false);
		}
		
		addMouseListener();
	}
	
	public ArrayList <Shop_Customization_PictureSettings> getCustomizations() {
		
		return customizations;
		
	}
	
	public void addCustomization(Shop_Customization_PictureSettings frontCustomization) {
		
		customizations.add(frontCustomization);
	}

	public ArrayList <Shop_Customization_TextSettings> getTextCustomizations() {
		return textCustomizations;
	}

	public void addTextCustomization(Shop_Customization_TextSettings textCustomizations) {
		this.textCustomizations.add(textCustomizations);
	}

	public boolean isFront() {
		return front;
	}

	public void setFront(boolean front) {
		this.front = front;
	}

	public boolean isBack() {
		return back;
	}

	public void setBack(boolean back) {
		this.back = back;
	}

	public void paintComponent(Graphics graphics){
		
		super.paintComponent(graphics);
		
		for(int i = 0; i < customizations.size(); i++){
			
			Shop_Customization_PictureSettings custImage = customizations.get(i);
			graphics.drawImage(custImage.getDesign(), custImage.getStartX(), custImage.getStartY(), custImage.getWidth(), custImage.getHeight(), this);
			
		}
		
		for(int i = 0; i < textCustomizations.size(); i++){
			
			Shop_Customization_TextSettings text = textCustomizations.get(i);
			graphics.setFont(text.getFont());
			graphics.setColor(Color.BLACK);
			graphics.drawString(text.getText(), text.getStartX(), text.getStartY());
		}
		
	}
	
	private void addMouseListener(){
		addMouseListener(new MouseListener(){
			public void mouseClicked(MouseEvent e) {
				Shop_Customization_Form.moveDone();
		
				if(front && Shop_Customization_Form.moveFrontCustomization){
					
					Shop_Customization_PictureSettings customizationToMove = customizations.get(customizations.indexOf(Shop_Customization_Form.customizationToMove));
					
					Shop_Customization_PictureSettings movedCustomization = new Shop_Customization_PictureSettings();
					movedCustomization.setDesign(customizationToMove.getDesign());
					movedCustomization.setStartX((e.getX() - (customizationToMove.getWidth() / 2)));
					movedCustomization.setStartY((e.getY() - (customizationToMove.getHeight() / 2)));
					movedCustomization.setWidth(customizationToMove.getWidth());
					movedCustomization.setHeight(customizationToMove.getHeight());
					
					customizations.set(customizations.indexOf(customizationToMove), movedCustomization);
					
					Shop_Customization_Form.moveFrontCustomization = false;
					
					repaint();
					
				}
				else if(back && Shop_Customization_Form.moveBackCustomization){
					
					Shop_Customization_PictureSettings customizationToMove = customizations.get(customizations.indexOf(Shop_Customization_Form.customizationToMove));
					
					Shop_Customization_PictureSettings movedCustomization = new Shop_Customization_PictureSettings();
					movedCustomization.setDesign(customizationToMove.getDesign());
					movedCustomization.setStartX((e.getX() - (customizationToMove.getWidth() / 2)));
					movedCustomization.setStartY((e.getY() - (customizationToMove.getHeight() / 2)));
					movedCustomization.setWidth(customizationToMove.getWidth());
					movedCustomization.setHeight(customizationToMove.getHeight());
					
					customizations.set(customizations.indexOf(customizationToMove), movedCustomization);
					
					Shop_Customization_Form.moveBackCustomization = false;
					
					repaint();
					
				}
				else if(front && Shop_Customization_Form.moveFrontTextCustomization){
					
					Shop_Customization_TextSettings textCustomizationToMove = textCustomizations.get(textCustomizations.indexOf(Shop_Customization_Form.textCustomizationToMove));
					
					Shop_Customization_TextSettings movedTextCustomization = new Shop_Customization_TextSettings();
					movedTextCustomization.setText(textCustomizationToMove.getText());
					movedTextCustomization.setFont(textCustomizationToMove.getFont());
					movedTextCustomization.setStartX(e.getX() - ((textCustomizationToMove.getText().length() * textCustomizationToMove.getFont().getSize())/4));
					movedTextCustomization.setStartY(e.getY());
					
					textCustomizations.set(textCustomizations.indexOf(Shop_Customization_Form.textCustomizationToMove), movedTextCustomization);
					
					Shop_Customization_Form.moveFrontTextCustomization = false;
					
					repaint();
					
				}
				else if(back && Shop_Customization_Form.moveBackTextCustomization){
					
					Shop_Customization_TextSettings textCustomizationToMove = textCustomizations.get(textCustomizations.indexOf(Shop_Customization_Form.textCustomizationToMove));
					
					Shop_Customization_TextSettings movedTextCustomization = new Shop_Customization_TextSettings();
					movedTextCustomization.setText(textCustomizationToMove.getText());
					movedTextCustomization.setFont(textCustomizationToMove.getFont());
					movedTextCustomization.setStartX(e.getX() - ((textCustomizationToMove.getText().length() * textCustomizationToMove.getFont().getSize())/4));
					movedTextCustomization.setStartY(e.getY());
					
					textCustomizations.set(textCustomizations.indexOf(Shop_Customization_Form.textCustomizationToMove), movedTextCustomization);
					
					Shop_Customization_Form.moveBackTextCustomization = false;
					
					repaint();
					
				}
				
			}
		
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}
